package com.ym.medical.service.impl;

import com.ym.core.utils.tkmybatis.BaseSVImpl;
import com.ym.medical.dao.DoctorDetailsDao;
import com.ym.medical.domain.entity.DoctorDetailsEntity;
import com.ym.medical.domain.entity.UserEntity;
import com.ym.medical.domain.param.DoctorDetailsParam;
import com.ym.medical.domain.param.DoctorSearchParam;
import com.ym.medical.domain.vo.DoctorDetailAllInfoVo;
import com.ym.medical.domain.vo.ResultDoctor;
import com.ym.medical.domain.vo.SimpleDoctorInfoVo;
import com.ym.medical.enums.AddressGroupAlgIdentEnum;
import com.ym.medical.enums.AddressGroupAlgTypeEnum;
import com.ym.medical.enums.DoctorServiceItemEnum;
import com.ym.medical.enums.PriceIntervalEnum;
import com.ym.medical.service.IDoctorDetailsSV;
import com.ym.medical.service.IDsAdviseRecordSV;
import com.ym.medical.service.ISysDictSV;
import com.ym.medical.service.IUserSV;
import com.ym.medical.service.IYunxinUserSV;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 医生详情Service业务层处理
 *
 * @author ym
 * @date 2020-08-19
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DoctorDetailsSVImpl extends BaseSVImpl<DoctorDetailsEntity, DoctorDetailsDao> implements IDoctorDetailsSV {

	@Autowired
	private IUserSV userSV;

	@Autowired
	private IYunxinUserSV yunxinUserSV;

	@Autowired
	private ISysDictSV sysDictSV;

	@Autowired
	private IDsAdviseRecordSV dsAdviseRecordSV;


	@Override
	public long save(DoctorDetailsEntity doctorDetails) {
		this.insert(doctorDetails);
		return doctorDetails.getId();
	}


	@Override
	public DoctorDetailsEntity findById(long doctorDetailsId) {
		return selectById(doctorDetailsId);
	}

	@Override
	public DoctorDetailsEntity findByModel(DoctorDetailsParam doctorDetailsModel) {
		List<DoctorDetailsEntity> doctorDetailss = list(doctorDetailsModel);
		return doctorDetailss.isEmpty() ? null : doctorDetailss.get(0);
	}

	@Override
	public List<DoctorDetailsEntity> list(DoctorDetailsParam doctorDetailsModel) {
		return this.selectList(doctorDetailsModel.covert());
	}


	@Override
	public List<ResultDoctor> listPagePlatformMembers(String keywords, Long userId) {
		List<ResultDoctor> list = mapper.listPlatformMembers(keywords, userId, AddressGroupAlgIdentEnum.THE_DOCTOR.getKey(), AddressGroupAlgTypeEnum.System.getKey());
		return list;
	}


	@Override
	public List<ResultDoctor> listStudioMembers(DoctorSearchParam query) {
		// 价格区间设置
		StringBuilder priceStr = new StringBuilder();
		String price = query.getPrice();
		String way = query.getWay();
		if (StringUtils.isNotBlank(price)) {
			String[] prices = price.split(",");
			// and( ( 图文价格1 and 图文价格 2)  or （电话价格1 and 电话价格2） )
			// or条件拼接
			if (StringUtils.isBlank(way) || way.indexOf(DoctorServiceItemEnum.BY_INQUIRING.getKey()) > -1) {
				priceStr.append(" ( ");
				for (int j = 0; j < prices.length; j++) {
					String p = prices[j];
					// 取出字典范围值，设置sql
					String dictValue = sysDictSV.getDictValue(PriceIntervalEnum.class, p);
					String[] values = dictValue.split("-");
					for (int i = 0; i < values.length; i++) {
						String value = values[i];
						// begin
						if (values.length == 2 && i == 0) {
							priceStr.append(" ( ");
						} else if (values.length == 1) {
							priceStr.append(" ( ");
						}
						if (i == 0 && NumberUtils.isDigits(value)) {
							priceStr.append(" l.dspPrice >= " + value);

						}
						//end
						if (i == 1 && NumberUtils.isDigits(value)) {
							priceStr.append(" and l.dspPrice <= " + value);
						}
						if (values.length == 2 && i == 1) {
							priceStr.append(" ) ");
						} else if (values.length == 1) {
							priceStr.append(" ) ");
						}
					}
					if (j != prices.length - 1) {
						priceStr.append(" or  ");
					}
				}
				priceStr.append(" ) ");
			}
			if (StringUtils.isBlank(way) || way.split(",").length > 1) {
				priceStr.append(" or ");
			}
			if (StringUtils.isBlank(way) || way.indexOf(DoctorServiceItemEnum.CALL_PHYSICIAN_VISITS.getKey()) > -1) {
				priceStr.append(" ( ");
				for (int j = 0; j < prices.length; j++) {
					String p = prices[j];
					// 取出字典范围值，设置sql
					String dictValue = sysDictSV.getDictValue(PriceIntervalEnum.class, p);
					String[] values = dictValue.split("-");

					for (int i = 0; i < values.length; i++) {
						String value = values[i];
						// begin
						if (values.length == 2 && i == 0) {
							priceStr.append(" ( ");
						} else if (values.length == 1) {
							priceStr.append(" ( ");
						}
						if (i == 0 && NumberUtils.isDigits(value)) {
							priceStr.append(" m.dspPrice >= " + value);

						}
						//end
						if (i == 1 && NumberUtils.isDigits(value)) {
							priceStr.append(" and m.dspPrice <= " + value);
						}
						if (values.length == 2 && i == 1) {
							priceStr.append(" ) ");
						} else if (values.length == 1) {
							priceStr.append(" ) ");
						}
					}
					if (j != prices.length - 1) {
						priceStr.append(" or  ");
					}
				}
				priceStr.append(" ) ");
			}
		}
		query.setPrice(priceStr.toString());


		List<ResultDoctor> list = mapper.listStudioMembers(query);
		return list;
	}

	@Override
	public Long listStudioMembersCount(DoctorSearchParam query) {
		return mapper.listStudioMembersCount(query);
	}

	@Override
	public List<DoctorDetailAllInfoVo> listDoctorAllInfo(DoctorDetailsParam doctorDetailsModel) {
		return mapper.listDoctorAllInfo(doctorDetailsModel);
	}

	@Override
	public void updateByDoctorId(DoctorDetailsEntity doctorDetails, UserEntity user) {
		if (doctorDetails.getId() != null) {
			doctorDetails.setModifier(doctorDetails.getId());
			doctorDetails.setModifyTime(new Date());
			this.updateById(doctorDetails);
		}
		if (user.getId() != null) {
			user.setModifier(user.getId());
			user.setModifyTime(new Date());
			userSV.updateById(user);
			//更新云信账户
			yunxinUserSV.updateYunxinAcc(user.getId());
		}
	}

	@Override
	public List<SimpleDoctorInfoVo> selectSimpleDoctorInfo(Long doctorId) {
		return mapper.selectSimpleDoctorInfo(doctorId);
	}
}
